/*
 * DbConfigView.java
 *
 * Created on 23 Eylül 2008 Salı, 15:06
 */
package org.testis.gui.mvc.view.testdesign;

import org.testis.gui.mvc.view.*;
import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.controller.testdesign.SelectSuiteController;
import org.testis.gui.mvc.utilities.CheckListItem;
import org.testis.gui.mvc.utilities.CheckListRenderer;
import org.testis.gui.mvc.utilities.Validatable;
import java.awt.Frame;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.util.LinkedList;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;

/**
 *
 * @author  nurhat
 */
public class SelectSuiteView extends AtofBaseDialog implements
        AbstractViewPanel,
        Validatable {

    private static final long serialVersionUID = 203213L;
    private SelectSuiteController m_controller;

    /** Creates new form DbConfigView */
    public SelectSuiteView(java.awt.Frame parent,
            boolean modal) {
        super(parent, modal);
        m_controller = TestDesignControllerList.newInstance().getSelectSuiteController();
        initComponents();
        setDefaultButton(btnClose);
    }

    public SelectSuiteView(Frame parent,
            boolean modal,
            boolean editMode) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnClose = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        suiteList = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        dynamicMapList = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select Tree Load");
        setLocationByPlatform(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        btnClose.setText("Continue");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Select Suites"));

        suiteList.setModel(new DefaultListModel());
        suiteList.setCellRenderer(new CheckListRenderer());
        suiteList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        ((DefaultListModel)suiteList.getModel()).addElement(new CheckListItem("Select / deselect all"));
        suiteList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                suiteListMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(suiteList);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Select Dynamic Maps"));

        dynamicMapList.setModel(new DefaultListModel());
        dynamicMapList.setCellRenderer(new CheckListRenderer());
        dynamicMapList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        ((DefaultListModel)dynamicMapList.getModel()).addElement(new CheckListItem("Select / deselect all"));
        dynamicMapList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dynamicMapListMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(dynamicMapList);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnClose)
                        .addGap(8, 8, 8))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addComponent(btnClose)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
    m_controller.operationSetSelectedTestSuites(getSelectedCheckListItems(
            suiteList));
    m_controller.operationSetSelectedDynamicMaps(getSelectedCheckListItems(
            dynamicMapList));
    dispose();
}//GEN-LAST:event_btnCloseActionPerformed

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    WindowListener[] listeners = getWindowListeners();
    for (int i = 0; i < listeners.length; i++) {
        this.removeWindowListener(listeners[i]);
    }
    m_controller.operationCloseWindow();
    dispose();
}//GEN-LAST:event_formWindowClosed

private void suiteListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_suiteListMouseClicked
    JList list = (JList) evt.getSource();

    // Get index of item clicked
    int index = list.locationToIndex(evt.getPoint());
    CheckListItem item = (CheckListItem) list.getModel().getElementAt(index);
    // Toggle selected state
    item.setSelected(!item.isSelected());

    if (index == 0) {
        boolean selectAll = item.isSelected();
        if (list.getModel().getSize() >= 2) {
            for (int i = 1; i < list.getModel().getSize(); i++) {
                CheckListItem item_i = (CheckListItem) list.getModel().getElementAt(
                        i);
                // Toggle selected state
                item_i.setSelected(selectAll);
            }
        }
        // Repaint list
        list.repaint();
    } else {
        // Repaint cell
        list.repaint(list.getCellBounds(index, index));
    }
}//GEN-LAST:event_suiteListMouseClicked

private void dynamicMapListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dynamicMapListMouseClicked
    JList list = (JList) evt.getSource();

    // Get index of item clicked
    int index = list.locationToIndex(evt.getPoint());
    CheckListItem item = (CheckListItem) list.getModel().getElementAt(index);
    // Toggle selected state
    item.setSelected(!item.isSelected());

    if (index == 0) {
        boolean selectAll = item.isSelected();
        if (list.getModel().getSize() >= 2) {
            for (int i = 1; i < list.getModel().getSize(); i++) {
                CheckListItem item_i = (CheckListItem) list.getModel().getElementAt(
                        i);
                // Toggle selected state
                item_i.setSelected(selectAll);
            }
        }
        // Repaint list
        list.repaint();
    } else {
        // Repaint cell
        list.repaint(list.getCellBounds(index, index));
    }
}//GEN-LAST:event_dynamicMapListMouseClicked

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(
                SelectSuiteController.VIEWVISIBILITY_PROPERTY)) {
            centerParent();
        } else if (evt.getPropertyName().equals(
                SelectSuiteController.LAST_LOADED_TESTSUITES_PROPERTY)) {
            LinkedList<String> newValue = (LinkedList<String>) evt.getNewValue();
            loadTestSuiteList(newValue);
        } else if (evt.getPropertyName().equals(
                SelectSuiteController.LAST_LOADED_DYNAMICMAPS_PROPERTY)) {
            LinkedList<String> newValue = (LinkedList<String>) evt.getNewValue();
            loadDynamicMapList(newValue);
        }
    }

    @Override
    public boolean validateForm() {
        boolean valid = true;
        return valid;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JList dynamicMapList;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList suiteList;
    // End of variables declaration//GEN-END:variables

    private void loadTestSuiteList(LinkedList<String> list) {
        if (list == null /*|| list.size() == 0*/) {
            return;
        }
        DefaultListModel model = (DefaultListModel) suiteList.getModel();
        model.clear();
        model.addElement(new CheckListItem("Select / deselect all"));
        for (int i = 0; i < list.size(); i++) {
            model.addElement(new CheckListItem(list.get(i), true));
        }
    }

    private void loadDynamicMapList(LinkedList<String> list) {
        if (list == null /*|| list.size() == 0*/) {
            return;
        }
        DefaultListModel model = (DefaultListModel) dynamicMapList.getModel();
        model.clear();
        model.addElement(new CheckListItem("Select / deselect all"));
        for (int i = 0; i < list.size(); i++) {
            model.addElement(new CheckListItem(list.get(i), true));
        }
    }

    private LinkedList<String> getSelectedCheckListItems(JList list) {
        LinkedList<String> result = new LinkedList<String>();
        int size = list.getModel().getSize();
        if (size >= 2) {
            for (int i = 1; i < size; i++) {
                CheckListItem item_i = (CheckListItem) list.getModel().getElementAt(
                        i);
                if (item_i.isSelected()) {
                    result.add(item_i.toString());
                }
            }
        }
        return result;
    }

    /*public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

    @Override
    public void run() {
    TestDesignControllerList.newInstance().getSelectSuiteController().startLoader();
    }
    });
    }*/
}
